<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">


</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统管理
            <small>菜单管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">


                <el-input placeholder="菜单名称" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="pagination.currentPage = 1; findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table
                    class="tree-table"
                    :data="dataList"
                    style="width: 100%;margin-bottom: 20px;"
                    row-key="id"
                    border
                    lazy
                    :tree-props="{children: 'children'}">
                <el-table-column
                        prop="name"
                        label="菜单名称"
                        sortable
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="linkUrl"
                        label="访问路径"
                        sortable
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="path"
                        label="菜单路径">
                </el-table-column>

                <el-table-column
                        prop="level"
                        label="菜单级别">
                </el-table-column>

                <el-table-column
                        prop="description"
                        label="菜单描述">
                </el-table-column>

                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="访问路径" prop="linkUrl">
                                    <el-input placeholder="最后一级菜单需要" v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>


                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="优先级低于" prop="priority">
                                    <el-select v-model="formData.priority" clearable style="width: 100%"
                                               placeholder="请选择在哪个菜单之后，不选择默认为第一个">
                                        <el-option
                                                v-for="item in menuOptions"
                                                :key="item.priority"
                                                :label="item.label"
                                                :value="item.priority">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>

                            <el-row>


                                <el-col :span="12">


                                    <el-form-item label="选择父菜单" prop="parentMenuId">
                                        <div class="block">
                                            <el-cascader
                                                    v-model="menuIDList"
                                                    ref="cascader"
                                                    :props="{ checkStrictly: true }"
                                                    :options="options"
                                                    filterable
                                                    clearable
                                                    @change="change"
                                            ></el-cascader>
                                        </div>
                                    </el-form-item>
                                </el-col>


                                <el-col :span="12">
                                    <el-form-item label="菜单级别" prop="level">
                                        <el-select v-model="formData.level" disabled placeholder="请选择">
                                            <el-option
                                                    v-for="level in levels"
                                                    :key="level"
                                                    :label="level"
                                                    :value="level">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                            </el-row>

                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="图标" prop="icon">
                                    <el-input v-model="formData.icon"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>

                            <el-col :span="24">
                                <el-form-item label="注意事项" prop="description">
                                    <el-input v-model="formData.description"/>
                                </el-form-item>
                            </el-col>
                        </el-row>


                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑菜单" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="访问路径" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>


                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="优先级低于" prop="priority">
                                    <el-select v-model="formData.priority" clearable style="width: 100%"
                                               placeholder="请选择在哪个菜单之前，不选择默认为第一个">
                                        <el-option
                                                v-for="item in menuOptions"
                                                :key="item.priority"
                                                :label="item.label"
                                                :value="item.priority">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>

                            <el-row>

                                <el-col :span="12">
                                    <el-form-item label="选择父菜单" prop="placeholder">
                                        <div class="block">
                                            <!--     v-model="formData.parentMenuId"
                                                    @click="editParentMenuId()"
                                                    -->
                                            <el-cascader

                                                    v-model="menuIDList"
                                                    ref="cascader"
                                                    :props="{ checkStrictly: true }"
                                                    :options="options"
                                                    filterable
                                                    clearable
                                                    @change="change"
                                            ></el-cascader>
                                        </div>


                                    </el-form-item>
                                </el-col>
                                <el-col :span="12">
                                    <el-form-item label="菜单级别" prop="level">
                                        <el-select v-model="formData.level" disabled placeholder="请选择">
                                            <el-option
                                                    v-for="level in levels"
                                                    :key="level"
                                                    :label="level"
                                                    :value="level">
                                            </el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                            </el-row>

                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="图标" prop="icon">
                                    <el-input v-model="formData.icon"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>

                            <el-col :span="24">
                                <el-form-item label="注意事项" prop="description">
                                    <el-input v-model="formData.description"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>

                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>

<!-- 引入样式 -->
<link rel="stylesheet" href="../plugins/element-ui/lib/theme-chalk/index.css">
<!-- 引入组件库 -->
<script src="../plugins/element-ui/lib/index.js"></script>

<script>            var vue = new Vue({
    el: '#app',
    data: {
        menuOptions: [],
        id: 999999,
        menuIDList: [],
        pagination: {currentPage: 1, pageSize: 10, total: 0, queryString: null},
        dataList: [],
        formData: {level: 1},
        dialogFormVisible: false,
        dialogFormVisible4Edit: false,
        rules: {
            name: [{required: true, message: '菜单名称为必填项', trigger: 'blur'}],
            path: [{required: true, message: '路径级别为必填项', trigger: 'blur'}],
            priority: [{required: true, message: '优先级必填项', trigger: 'blur'}],
            level: [{required: true, message: '菜单级别为必填项', trigger: 'blur'}],
            parentMenuId: [{required: true, message: '自关联Id别为必填项', trigger: 'blur'}]
        },
        options: [],
        levels: [1, 2, 3]
    },
    created() {
        this.findPage();
        // axios.get("/user/findSidebar.do").then((resp) => {
        //     this.menuList = resp.data.data;
        // });
    },
    methods: {
        change(val) {
            let nodesObj = this.$refs['cascader'].getCheckedNodes();

            if (nodesObj[0] == null || nodesObj[0] == undefined || nodesObj[0].value == null) {
                this.id = 999999;
                console.log(this.id);
                this.formData.level = 1;
            } else {
                this.id = nodesObj[0].value;
                this.formData.level = nodesObj[0].level + 1;
                console.log(this.id);
            }

            //以下是设置选择的父菜单的所有子菜单
            if (this.formData.level == 1) {
                this.menuOptions = this.options;
            }

            if (this.formData.level == 2) {
                for (let i = 0; i < this.options.length; i++) {
                    if (this.id == this.options[i].value) {
                        this.menuOptions = this.options[i].children;
                    }
                }
            }

            if (this.formData.level == 3) {
                for (let i = 0; i < this.options.length; i++) {
                    if (this.options[i].children) {
                        for (let j = 0; j < this.options[i].children.length; j++) {
                            if (this.id == this.options[i].children[j].value) {
                                this.menuOptions = this.options[i].children[j].children;
                            }
                        }
                    }

                }
            }
            if (this.formData.priority) {
                this.formData.priority = undefined;
            }

        }, showMessage(r) {
            if (r == 'Error: Request failed with status code 403') {
                this.$message.error('无访问权限');
                return;
            } else {
                this.$message.error('未知错误');
                return;
            }
        },
        handleEdit() {
            if (this.formData.parentMenuId) {
                if (this.formData.parentMenuId.length == 1) {
                    this.formData.parentMenuId = this.formData.parentMenuId[0]
                }
                if (this.formData.parentMenuId.length == 2) {
                    this.formData.parentMenuId = this.formData.parentMenuId[1]
                }
                if (this.formData.parentMenuId.length == 3) {
                    this.formData.parentMenuId = this.formData.parentMenuId[2]
                }
                if (this.formData.parentMenuId.length == 4) {
                    this.formData.parentMenuId = this.formData.parentMenuId[3]
                }
            }
            ;
            let param = {
                description: this.formData.description,
                icon: this.formData.icon,
                id: this.formData.id,
                level: this.formData.level,
                linkUrl: this.formData.linkUrl,
                name: this.formData.name,
                path: this.formData.path,
                priority: this.formData.priority,
                parentMenuId: this.id
            };
            axios.post("/menu/edit.do?id=" + this.id, param).then((resp) => {
                if (resp.data.flag) {
                    this.$message({message: resp.data.message, type: "success"});
                } else {
                    this.$message.error(resp.data.message);
                }
            }).finally(() => {
                this.dialogFormVisible4Edit = false;
                this.findPage();
            });
        },
        handleAdd() {
            axios.post("/menu/add.do?id=" + this.id, this.formData).then((res) => {
                this.dialogFormVisible = false;
                if (res.data.flag) {
                    this.findPage();
                    this.$message({
                        message: res.data.message,
                        type: 'success'
                    });
                } else {
                    this.$message.error(res.data.message);
                }
            });
        }
        , findPage() {
            let param = {
                currentPage: this.pagination.currentPage,
                pageSize: this.pagination.pageSize,
                queryString: this.pagination.queryString
            };
            axios.post("/menu/pageQuery.do", param).then((resp) => {
                this.pagination.total = resp.data.total;
                this.dataList = resp.data.rows;
            });
        },
        resetForm() {
            this.formData = {level: 1};
            this.options = [];
            this.menuIDList = [];
            this.$refs.cascader.$refs.panel.clearCheckedNodes();
            this.$refs.cascader.$refs.panel.activePath = [];
        },
        handleCreate() {
            debugger;
            axios.post("/menu/findMenuparentMenuId.do").then((resp) => {
                if (resp.data.flag) {
                    this.options = resp.data.data;
                    this.menuOptions = resp.data.data;
                    console.log(this.options);
                } else {
                    this.$message.error(resp.data.message);
                }
            });
            this.dialogFormVisible = true;
            if (this.$refs['dataAddForm']) {
                this.$refs['dataAddForm'].resetFields();
            }
            this.resetForm();
        },
        handleUpdate(row) {
            if (this.$refs.dataEditForm) {
                this.$refs.dataEditForm.resetFields();
            }
            this.dialogFormVisible4Edit = true;
            axios.get("/menu/findById.do?id=" + row.id).then((resp) => {
                if (resp.data.flag) {
                    this.formData = resp.data.data;

                    //this.menuIDList = [ ];
                    axios.get("/menu/findParentIdsByMenuId.do?mid=" + row.id).then((res) => {
                        this.menuIDList = res.data;
                    });


                    /*axios.post("/menu/findMenuIdAndDisabled.do?id=" + row.id)
                        .then((resp) => {
                            if (resp.data.flag) {
                                this.options = resp.data.data;
                                let pid = this.formData.parentMenuId;
                                if (this.formData.level == 1) {
                                    this.menuOptions = this.options;
                                }

                                if (this.formData.level == 2) {
                                    for (let i = 0; i < this.options.length; i++) {
                                        if (pid == this.options[i].value) {
                                            this.menuOptions = this.options[i].children;
                                        }
                                    }
                                }

                                if (this.formData.level == 3) {
                                    for (let i = 0; i < this.options.length; i++) {
                                        if (this.options[i].children) {
                                            for (let j = 0; j < this.options[i].children.length; j++) {
                                                if (pid == this.options[i].children[j].value) {
                                                    this.menuOptions = this.options[i].children[j].children;
                                                }
                                            }
                                        }

                                    }
                                }


                            } else {
                                this.$message.error(resp.data.message);
                            }
                        });*/
                    //我后加的，不知道对不对
                    axios.post("/menu/findMenuparentMenuId.do").then((resp) => {
                        if (resp.data.flag) {
                            this.options = resp.data.data;
                            this.menuOptions = resp.data.data;
                            console.log(this.options);
                        } else {
                            this.$message.error(resp.data.message);
                        }
                    });

                } else {
                    this.message.error(resp.data.message);
                }
            });
        }
        , handleCurrentChange(currentPage) {
            this.pagination.currentPage = currentPage;
            this.findPage();
        }, handleDelete(row) {
            this.$confirm("你确定要删除" + row.name + "的全部信息吗？", "提示", {type: 'warnig'}).then(() => {
                axios.get("/menu/delete.do?id=" + row.id).then((res) => {
                    if (res.data.flag) {
                        this.$message({type: 'success', message: res.data.message});
                        this.findPage();
                    } else {
                        this.$message.error(res.data.message);
                    }
                }).catch((r) => {
                    this.showMessage(r);
                });
            }).catch(() => {
                this.$message({type: 'info', message: '操作已取消'});
            });
        }
    }, watch: {
        menuIDList() {
            if (this.$refs.cascader) {
                this.$refs.cascader.dropDownVisible = false
            }
        }, formData() {
            if (this.$refs.cascader) {
                this.$refs.cascader.dropDownVisible = false
            }
        }
    }
})    </script>
</html>